'''
 @Author: 王谦璘
 @Date: 2023/04/04/16:46
 @Description: 里程校正算法（基于公里标配准）
'''
# -*- coding: utf8 -*-
import sys
import pandas as pd
# import datetime
def mileage_correction(filepath):  # filepath:文件路径,String类型
    """里程校正"""
    data = pd.read_excel(filepath)
    # 提取数据
    mileage_base = data.loc[:, "标准公里标"]
    mileage_detect = data.loc[:, "公里标"]

    # 数据预处理:
    # 1.删除重复值
    mileage_base = mileage_base.drop_duplicates(keep="first", inplace=False)

    # 2.删除空值
    mileage_base = list(mileage_base.dropna())

    # 3.将列表中的"nan"转换为”None"
    mileage_detect = list(mileage_detect)
    mileage_detect = [x if not pd.isnull(x) else None for x in mileage_detect]

    # 进行公里标配准
    for i in range(len(mileage_detect)):
        if mileage_detect[i] is not None:
            for j in range(len(mileage_base)):
                if abs(mileage_detect[i] - mileage_base[j]) <= 0.015:
                    mileage_detect[i] = mileage_base[j]
                    break
    data["修正后公里标"] = mileage_detect
    return data
if __name__ == "__main__":
    filepath = sys.argv[1]
    # filepath = "D:/数据/供电数据/01-石长线/石长线-上行-捞刀河站-石门县南站-2020-06-09.xlsx"
    data_new = mileage_correction(filepath)
    # print(datetime.datetime.now(),"\n")
    savepath = filepath.split(".")[0] + "_revised"
    data_new.to_excel(savepath + ".xlsx")
    print("1")